package com.itheima.travel;

import com.itheima.travel.config.SpringMvcConfig;
import com.itheima.travel.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

/*
* 声明配置类，继承SpringMvc配置类
* 扫描web下面的包
* 自定义的拦截器引入
* 重写addInterceptors，（添加拦截器+拦截路径）
* */
@Configuration
@ComponentScan(value = {"com.itheima.travel.web","springfox.documentation.swagger.web"})
public class PlatformSpringMvcConfig extends SpringMvcConfig {

    @Bean("loginInterceptor")
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        //添加拦截器，指定拦截路径
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/**/user/**",
                        "/**/seller/**",
                        "/**/category/**",
                        "/**/route/**",
                        "/**/webjars/**",
                        "/**/swagger-ui.html",
                        "/**/swagger-resources/**",
                        "/**/v2/**"
                );
    }

    /**
     * 资源路径 映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 支持webjars
         */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        /**
         * 支持swagger
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }
}
